package com.etone.smartAudit.onlyoffice.entity;

import cn.hutool.core.util.IdUtil;
import cn.hutool.crypto.digest.DigestUtil;
import com.alibaba.fastjson.JSON;
import com.etone.smartAudit.onlyoffice.entity.dto.OnlyofficeDTO;
import com.etone.smartAudit.onlyoffice.util.DocumentManager;
import com.etone.smartAudit.onlyoffice.util.FileUtility;
import com.etone.smartAudit.onlyoffice.util.ServiceConverter;
import lombok.Data;

import java.util.HashMap;
import java.util.Map;

@Data
public class OnlyofficeFileModel {
    public String type = "desktop";
    public String documentType;
    public Document document;
    public EditorConfig editorConfig;
    public String token;

    public OnlyofficeFileModel(UserBean loginUser, OnlyofficeDTO onlyofficeDTO, String mode) {
        String type = onlyofficeDTO.getType();
        String fileId = onlyofficeDTO.getFileId();
        String fileName = onlyofficeDTO.getFileName();
        String filePath = onlyofficeDTO.getFilePath();
        String fileUrl = onlyofficeDTO.getFileUrl();
        if (fileName == null) {
            fileName = "";
        }
        fileName = fileName.trim();
        documentType = FileUtility.GetFileType(fileName).toString().toLowerCase();
        document = new Document();
        document.title = fileName;
//        String suffix = fileUrl.substring(fileUrl.lastIndexOf(".") + 1);
//        System.out.println(suffix);
        document.fileType = FileUtility.GetFileExtension(fileName);
        document.url = fileUrl;
        editorConfig = new EditorConfig();
        editorConfig.mode = mode;
        editorConfig.callbackUrl = "";
        editorConfig.user.id = String.valueOf(loginUser.getUserId());
        editorConfig.user.name = loginUser.getUsername();
        String userId = DocumentManager.CurUserHostAddress(null);
//        document.key = ServiceConverter.GenerateRevisionId(userId + "/" + fileName);
//        document.key = DigestUtil.md5Hex(fileName + fileId);
//        document.key = IdUtil.fastSimpleUUID();
        document.key = onlyofficeDTO.getOfficeKey();
        editorConfig.customization.goback.url = onlyofficeDTO.getGobackUrl();
    }

    public void InitDesktop() {
        type = "embedded";
        editorConfig.InitDesktop(document.url);
    }

    public void BuildToken() {
        Map<String, Object> map = new HashMap<>();
        map.put("type", type);
        map.put("documentType", documentType);
        map.put("document", document);
        map.put("editorConfig", editorConfig);

        token = DocumentManager.CreateToken(map);
    }

    public class Document {
        public String title;
        public String url;
        public String fileType;
        public String key;
    }

    public class EditorConfig {
        public String mode = "edit";
        public String callbackUrl;
        public User user;
        public Customization customization;
        public Embedded embedded;
        public String lang = "zh-CN";
        public Plugins plugins;

        public EditorConfig() {
            user = new User();
            customization = new Customization();
            plugins = new Plugins();
            String[] autostart = {"asc.{7327FC95-16DA-41D9-9AF2-0E7F449F687D}"};
            plugins.autostart = autostart;
        }

        public void InitDesktop(String url) {
            embedded = new Embedded();
            embedded.saveUrl = url;
            embedded.embedUrl = url;
            embedded.shareUrl = url;
            embedded.toolbarDocked = "top";
        }

        public class User {
            public String id;
            public String name = " ";//Administrator
        }

        public class Customization {
            public Goback goback;
            public boolean forcesave = true;//是否启动强制保存
            public boolean autosave = true;//是否启动自动保存
            public boolean spellcheck = true;//关闭拼写检查

            public Customization() {
                goback = new Goback();
            }

            public class Goback {
                public String url;
            }
        }

        public class Embedded {
            public String saveUrl;
            public String embedUrl;
            public String shareUrl;
            public String toolbarDocked;
        }
    }


    public static String Serialize(OnlyofficeFileModel model) {
        return JSON.toJSONString(model);
    }

    public class Plugins {
        public String[] pluginsData;
        public String[] autostart;
    }
}
